import modelExtend from 'dva-model-extend'
import { lwjPageModel } from 'utils/model'
import { query, distributorQuery, queryByDistributor, add, detail, approve, deny, listDistributors } from './services/coupons'
import { routerRedux } from 'dva/router';


export default modelExtend(lwjPageModel, {
  namespace: 'coupon',
  state: {
    currentItem: {},
    pageOptions: {
      sort: 'createTime',
      order: 'DESC'
    },
    isExpand: false,
    visible: false,
    detailVisible: false,
    auditVisible: false,
    queryParam: {},
    currentSelect: [{}],
    optionType: '',
    distributorList: [{}],
    currentDistributor: {},
    usedAmount: '',
    auditType: '',
    fileVisible: true
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/marketing/coupon') {
          dispatch({ type: 'query', payload: {} })
          dispatch({ type: 'closeAllModal', payload: {} })
        }
        if (pathname === '/marketing/coupon/add') {
          dispatch({ type: 'distributorQuery', payload: {} })
        }
        if (pathname === '/marketing/coupon/listByDistributor') {
          dispatch({ type: 'queryDistributorAll', payload: {} })

          // dispatch({ type: 'queryByDistributor', payload: {} })
        }

      })
    }
  },
  effects: {
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ coupon }) => coupon.pageOptions);
      const queryParam = yield select(({ coupon }) => coupon.queryParam);

      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam
        },
        // ...payload
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    * distributorQuery({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ coupon }) => coupon.pageOptions);
      const queryParam = yield select(({ coupon }) => coupon.queryParam);

      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam
        },
        // ...payload
      };
      const data = yield call(distributorQuery, queryPayload);
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    * queryByDistributor({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ coupon }) => coupon.pageOptions);
      const queryParam = yield select(({ coupon }) => coupon.queryParam);
      const currentDistributor = yield select(({ coupon }) => coupon.currentDistributor);

      const queryPayload = {
        ...pageOptions,
        queryParam: {
          distributorId: currentDistributor.id,
          ...queryParam
        },
        // ...payload
      };
      const data = yield call(queryByDistributor, queryPayload);
      yield put({
        type: 'setUsedAmount',
        payload: data.result.usedAmount
      })
      const result = data.result.list;
      yield put({
        type: 'querySuccess',
        payload: {
          ...result
        }
      })
    },
    * toAdd({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/marketing/coupon/add`));
    },
    * add({
      payload
    }, { select, call, put }) {
      yield call(add, payload);
      yield put(routerRedux.push('/success', { listUrl: '/marketing/coupon', detailUrl: '/marketing/coupon' }));
    },
    * audit({
      payload
    }, { select, call, put }) {
      yield call(approve, payload.id, payload.values);
      yield put(routerRedux.push('/success', { listUrl: '/marketing/coupon', detailUrl: '/marketing/coupon' }));
    },
    * deny({
      payload
    }, { select, call, put }) {
      yield call(deny, payload);
      yield put(routerRedux.push('/success', { listUrl: '/marketing/coupon', detailUrl: '/marketing/coupon' }));
    },
    * queryDistributorAll({
      payload
    }, { select, call, put, take }) {
      const data = yield call(listDistributors, payload);
      const result = data.list
      if (!_.isEmpty(data)) {
        yield put({
          type: 'setDistributorList',
          payload: result
        })
        if (result.length > 0) {
          yield put({
            type: 'setCurrentDistributor',
            payload: result[0]
          })
          yield put({ type: 'queryByDistributor' })
        }
      }
    },
    * detail({
      payload
    }, { select, call, put }) {
      const data = yield call(detail, payload);
      yield put({
        type: 'setCurrentItem',
        payload: data.result
      })
    },
  },
  reducers: {
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    setVisible(state, { payload }) {
      return {
        ...state,
        visible: payload
      }
    },
    setSelectRecord(state, { payload }) {
      return {
        ...state,
        currentSelect: payload
      }
    },
    setDetailVisible(state, { payload }) {
      return {
        ...state,
        detailVisible: payload
      }
    },
    setAuditVisible(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    setCurrentItem(state, { payload }) {
      return {
        ...state,
        currentItem: payload
      }
    },
    setCurrentDistributor(state, { payload }) {
      return {
        ...state,
        currentDistributor: payload
      }
    },
    setDistributorList(state, { payload }) {
      return {
        ...state,
        distributorList: payload
      }
    },
    setOptionType(state, { payload }) {
      return {
        ...state,
        optionType: payload
      }
    },
    setUsedAmount(state, { payload }) {
      return {
        ...state,
        usedAmount: payload
      }
    },
    closeAllModal(state, { payload }) {
      return {
        ...state,
        auditVisible: false,
        detailVisible: false
      }
    },
    setAuditType(state, { payload }) {
      return {
        ...state,
        auditType: payload
      }
    },
    setFileVisible(state, { payload }) {
      return {
        ...state,
        fileVisible: payload
      }
    },
  }
})
